package at.ac.univie.zsu.aguataplan.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

/**
 * @author gerry
 * 
 */
@Entity
public class Game extends Tournament {

	@Column(nullable = false)
	private String gameResult;

	@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
	private Team refTeam1;

	@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.EAGER)
	private Team refTeam2;

	@OneToMany(cascade = CascadeType.ALL)
	private Set<Plays> refPlays = new HashSet<Plays>();

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public Game() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	public void addTeams(Team team1, Team team2) {
		this.refTeam1 = team1;
		this.refTeam2 = team2;
	}

	public void deleteTeams() {
		refTeam1 = null;
		refTeam2 = null;
	}

	public String toString() {
		return this.description + " - " + this.gameResult;
	}

	public String toStringExtended() {
		StringBuilder sb = new StringBuilder();
		sb.append("CalDate: ").append(getCalDate()).append(", ");
		sb.append("Description: ").append(getDescription()).append(", ");
		sb.append("GameResult: ").append(getGameResult()).append(", ");
		sb.append("Id: ").append(getId()).append(", ");
		sb.append("RefPlays: ").append(getRefPlays() == null ? "null" : getRefPlays().size()).append(", ");
		// sb.append("RefTeam: ").append(getRefTeam() == null ? "null" :
		// getRefTeam().size()).append(", ");
		sb.append("Team1: ").append(getRefTeam1()).append(", ");
		sb.append("Team2: ").append(getRefTeam2()).append(", ");
		sb.append("RefTournamentLocation: ").append(getRefTournamentLocation()).append(", ");
		sb.append("RefVideo: ").append(getRefVideo() == null ? "null" : getRefVideo().size()).append(", ");
		sb.append("Version: ").append(getVersion());
		return sb.toString();
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public String getGameResult() {
		return gameResult;
	}

	public void setGameResult(String gameResult) {
		this.gameResult = gameResult;
	}

	public Set<Plays> getRefPlays() {
		return refPlays;
	}

	public void setRefPlays(Set<Plays> refPlays) {
		this.refPlays = refPlays;
	}

	public Team getRefTeam1() {
		return refTeam1;
	}

	public void setRefTeam1(Team refTeam1) {
		this.refTeam1 = refTeam1;
	}

	public Team getRefTeam2() {
		return refTeam2;
	}

	public void setRefTeam2(Team refTeam2) {
		this.refTeam2 = refTeam2;
	}

}
